package com.example.demo.service;

import com.example.demo.utils.JsonUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author linjingze
 * @date 2023/12/19 4:11 PM
 */
public class DeliveryPriceConfig {

    private static final String XSD_PRICE = "{\"boardOfCar\":28,\"area\":\"休斯顿\",\"warehouseAreaList\":[{\"warehouse\":\"FTW1\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":45}],\"fullPrice\":1150},{\"warehouse\":\"HOU8\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":55}],\"fullPrice\":650},{\"warehouse\":\"HOU7\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":55}],\"fullPrice\":650},{\"warehouse\":\"HOU3\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":55}],\"fullPrice\":550},{\"warehouse\":\"HOU2\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":55}],\"fullPrice\":550},{\"warehouse\":\"HOU1\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":55}],\"fullPrice\":550},{\"warehouse\":\"SAT1\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":65}],\"fullPrice\":1350},{\"warehouse\":\"FTW9\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":65}],\"fullPrice\":1050},{\"warehouse\":\"FTW5\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":65}],\"fullPrice\":1150},{\"warehouse\":\"FTW3\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":65}],\"fullPrice\":1050},{\"warehouse\":\"FTW2\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":65}],\"fullPrice\":1350},{\"warehouse\":\"DFW7\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":65}],\"fullPrice\":1350},{\"warehouse\":\"DFW6\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":65}],\"fullPrice\":1350},{\"warehouse\":\"SAT6\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":85}],\"fullPrice\":1050},{\"warehouse\":\"SAT4\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":85}],\"fullPrice\":1350},{\"warehouse\":\"MEM1\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":105}],\"fullPrice\":1950},{\"warehouse\":\"MEM6\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":135}],\"fullPrice\":1950},{\"warehouse\":\"LFT1\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":135}],\"fullPrice\":1750},{\"warehouse\":\"OKC2\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":145}],\"fullPrice\":1950},{\"warehouse\":\"LIT2\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":145}],\"fullPrice\":1950},{\"warehouse\":\"ICT2\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":175}],\"fullPrice\":1950},{\"warehouse\":\"AMA1\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":155}]},{\"warehouse\":\"IAH3\",\"areaPriceList\":[{\"min\":1,\"max\":28,\"amount\":65}],\"fullPrice\":750}]}";
    public static final String DELIVERY_PRICE_CONFIG = "{\"boardOfCar\":28,\"area\":\"纽约\",\"warehouseAreaList\":[{\"warehouse\":\"ABE2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":55},{\"min\":7,\"max\":15,\"amount\":45}],\"fullPrice\":600},{\"warehouse\":\"ABE3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":55},{\"min\":7,\"max\":15,\"amount\":45}],\"fullPrice\":600},{\"warehouse\":\"ABE4\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":55},{\"min\":7,\"max\":15,\"amount\":45}],\"fullPrice\":700},{\"warehouse\":\"ABE8\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":40},{\"min\":7,\"max\":15,\"amount\":35}],\"fullPrice\":600},{\"warehouse\":\"ACY2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":40},{\"min\":7,\"max\":15,\"amount\":35}],\"fullPrice\":600},{\"warehouse\":\"ALB1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":75},{\"min\":7,\"max\":15,\"amount\":65}],\"fullPrice\":1150},{\"warehouse\":\"AVP1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":60},{\"min\":7,\"max\":15,\"amount\":50}],\"fullPrice\":950},{\"warehouse\":\"AVP3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":60},{\"min\":7,\"max\":15,\"amount\":50}],\"fullPrice\":950},{\"warehouse\":\"AVP9\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":60},{\"min\":7,\"max\":15,\"amount\":50}],\"fullPrice\":950},{\"warehouse\":\"BDL3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":95},{\"min\":7,\"max\":15,\"amount\":85}],\"fullPrice\":1100},{\"warehouse\":\"BDL6\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":95},{\"min\":7,\"max\":15,\"amount\":85}],\"fullPrice\":1100},{\"warehouse\":\"BNA2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":155},{\"min\":7,\"max\":15,\"amount\":140}],\"fullPrice\":2400},{\"warehouse\":\"BOS7\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":110},{\"min\":7,\"max\":15,\"amount\":100}],\"fullPrice\":1350},{\"warehouse\":\"BWI2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":110},{\"min\":7,\"max\":15,\"amount\":90}],\"fullPrice\":1350},{\"warehouse\":\"BWI4\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":110},{\"min\":7,\"max\":15,\"amount\":90}],\"fullPrice\":1350},{\"warehouse\":\"CHA2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":145},{\"min\":7,\"max\":15,\"amount\":135}],\"fullPrice\":2200},{\"warehouse\":\"CLT2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":130},{\"min\":7,\"max\":15,\"amount\":115}],\"fullPrice\":1750},{\"warehouse\":\"CLT3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":130},{\"min\":7,\"max\":15,\"amount\":115}],\"fullPrice\":1750},{\"warehouse\":\"CMH2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":120},{\"min\":7,\"max\":15,\"amount\":110}],\"fullPrice\":1400},{\"warehouse\":\"CMH3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":130},{\"min\":7,\"max\":15,\"amount\":120}],\"fullPrice\":1800},{\"warehouse\":\"CMH6\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":120},{\"min\":7,\"max\":15,\"amount\":110}],\"fullPrice\":1400},{\"warehouse\":\"DCA6\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":70},{\"min\":7,\"max\":15,\"amount\":60}],\"fullPrice\":1050},{\"warehouse\":\"DET1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":130},{\"min\":7,\"max\":15,\"amount\":120}],\"fullPrice\":1750},{\"warehouse\":\"GSO1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":120},{\"min\":7,\"max\":15,\"amount\":110}],\"fullPrice\":1650},{\"warehouse\":\"GSP1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":140},{\"min\":7,\"max\":15,\"amount\":130}],\"fullPrice\":1950},{\"warehouse\":\"HGR2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":85},{\"min\":7,\"max\":15,\"amount\":75}],\"fullPrice\":950},{\"warehouse\":\"IND2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":130},{\"min\":7,\"max\":15,\"amount\":120}],\"fullPrice\":1750},{\"warehouse\":\"JAX3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":200},{\"min\":7,\"max\":15,\"amount\":190}],\"fullPrice\":3500},{\"warehouse\":\"KRB2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":110},{\"min\":7,\"max\":15,\"amount\":100}],\"fullPrice\":1350},{\"warehouse\":\"MCO2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":200},{\"min\":7,\"max\":15,\"amount\":190}],\"fullPrice\":3750},{\"warehouse\":\"MDT1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":75},{\"min\":7,\"max\":15,\"amount\":65}],\"fullPrice\":1050},{\"warehouse\":\"MDT2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":75},{\"min\":7,\"max\":15,\"amount\":65}],\"fullPrice\":750},{\"warehouse\":\"MDW2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":165},{\"min\":7,\"max\":15,\"amount\":155}],\"fullPrice\":1950},{\"warehouse\":\"MEM1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":160},{\"min\":7,\"max\":15,\"amount\":145}],\"fullPrice\":2600},{\"warehouse\":\"MGE3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":155},{\"min\":7,\"max\":15,\"amount\":140}],\"fullPrice\":2550},{\"warehouse\":\"PHL4\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":70},{\"min\":7,\"max\":15,\"amount\":60}],\"fullPrice\":1050},{\"warehouse\":\"PHL5\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":70},{\"min\":7,\"max\":15,\"amount\":60}],\"fullPrice\":1050},{\"warehouse\":\"PHL6\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":70},{\"min\":7,\"max\":15,\"amount\":60}],\"fullPrice\":1050},{\"warehouse\":\"RIC1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":110},{\"min\":7,\"max\":15,\"amount\":100}],\"fullPrice\":1350},{\"warehouse\":\"RIC2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":110},{\"min\":7,\"max\":15,\"amount\":100}],\"fullPrice\":1350},{\"warehouse\":\"SAV3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":170},{\"min\":7,\"max\":15,\"amount\":150}],\"fullPrice\":2550},{\"warehouse\":\"SWF1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":75},{\"min\":7,\"max\":15,\"amount\":65}],\"fullPrice\":900},{\"warehouse\":\"ILG1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":70},{\"min\":7,\"max\":15,\"amount\":60}],\"fullPrice\":750},{\"warehouse\":\"TEB3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":45},{\"min\":7,\"max\":15,\"amount\":40}],\"fullPrice\":650},{\"warehouse\":\"TEB4\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":45},{\"min\":7,\"max\":15,\"amount\":40}],\"fullPrice\":650},{\"warehouse\":\"TEB6\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":40},{\"min\":7,\"max\":15,\"amount\":35}],\"fullPrice\":600},{\"warehouse\":\"TEB9\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":40},{\"min\":7,\"max\":15,\"amount\":35}],\"fullPrice\":600},{\"warehouse\":\"TPA2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":200},{\"min\":7,\"max\":15,\"amount\":190}],\"fullPrice\":3500},{\"warehouse\":\"TPA3\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":200},{\"min\":7,\"max\":15,\"amount\":190}],\"fullPrice\":3500},{\"warehouse\":\"TTN2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":40},{\"min\":7,\"max\":15,\"amount\":35}],\"fullPrice\":600},{\"warehouse\":\"XLX6\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":130},{\"min\":7,\"max\":15,\"amount\":115}],\"fullPrice\":1750},{\"warehouse\":\"XLX1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":85},{\"min\":7,\"max\":15,\"amount\":75}],\"fullPrice\":950},{\"warehouse\":\"CHO1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":110},{\"min\":7,\"max\":15}],\"fullPrice\":1350},{\"warehouse\":\"HSV1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":160},{\"min\":7,\"max\":15,\"amount\":145}],\"fullPrice\":2600},{\"warehouse\":\"ORF2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":135},{\"min\":7,\"max\":15,\"amount\":130}],\"fullPrice\":1450},{\"warehouse\":\"SWF2\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":80},{\"min\":7,\"max\":15,\"amount\":70}],\"fullPrice\":1000},{\"warehouse\":\"AKR1\",\"areaPriceList\":[{\"min\":1,\"max\":6,\"amount\":120},{\"min\":7,\"max\":15,\"amount\":110}],\"fullPrice\":1400},{\"warehouse\":\"KY1\",\"areaPriceList\":[{\"min\":1,\"max\":2147483647,\"amount\":135}],\"fullPrice\":2147483647}]}";
    private static final Map<String, String> configMap = new HashMap<>();
    public static final Map<String, String> nameMap = new HashMap<>();
    private static final Map<String, BigDecimal> trailerFeeMap = new HashMap<>();
    static {
        configMap.put("芝加哥", "{\"boardOfCar\":2147483647,\"area\":\"芝加哥\",\"warehouseAreaList\":[{\"warehouse\":\"CMH2\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":105},{\"min\":4,\"max\":6,\"amount\":85},{\"min\":7,\"max\":10,\"amount\":80},{\"min\":11,\"max\":2147483647,\"amount\":75}]},{\"warehouse\":\"CMH3\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":105},{\"min\":4,\"max\":6,\"amount\":85},{\"min\":7,\"max\":10,\"amount\":80},{\"min\":11,\"max\":2147483647,\"amount\":75}]},{\"warehouse\":\"DET1\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":105},{\"min\":4,\"max\":6,\"amount\":85},{\"min\":7,\"max\":10,\"amount\":80},{\"min\":11,\"max\":2147483647,\"amount\":75}]},{\"warehouse\":\"DET2\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":105},{\"min\":4,\"max\":6,\"amount\":85},{\"min\":7,\"max\":10,\"amount\":80},{\"min\":11,\"max\":2147483647,\"amount\":75}]},{\"warehouse\":\"FOE1\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":130},{\"min\":4,\"max\":6,\"amount\":125},{\"min\":7,\"max\":10,\"amount\":120},{\"min\":11,\"max\":2147483647,\"amount\":105}]},{\"warehouse\":\"FWA4\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":85},{\"min\":4,\"max\":6,\"amount\":80},{\"min\":7,\"max\":10,\"amount\":75},{\"min\":11,\"max\":2147483647,\"amount\":70}]},{\"warehouse\":\"IGQ2\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":65},{\"min\":4,\"max\":6,\"amount\":55},{\"min\":7,\"max\":10,\"amount\":45},{\"min\":11,\"max\":2147483647,\"amount\":40}]},{\"warehouse\":\"IND3\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":85},{\"min\":4,\"max\":6,\"amount\":80},{\"min\":7,\"max\":10,\"amount\":75},{\"min\":11,\"max\":2147483647,\"amount\":70}]},{\"warehouse\":\"IND2\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":85},{\"min\":4,\"max\":6,\"amount\":80},{\"min\":7,\"max\":10,\"amount\":75},{\"min\":11,\"max\":2147483647,\"amount\":70}]},{\"warehouse\":\"IND4\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":85},{\"min\":4,\"max\":6,\"amount\":80},{\"min\":7,\"max\":10,\"amount\":75},{\"min\":11,\"max\":2147483647,\"amount\":70}]},{\"warehouse\":\"IND9\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":85},{\"min\":4,\"max\":6,\"amount\":80},{\"min\":7,\"max\":10,\"amount\":75},{\"min\":11,\"max\":2147483647,\"amount\":70}]},{\"warehouse\":\"MQJ1\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":85},{\"min\":4,\"max\":6,\"amount\":80},{\"min\":7,\"max\":10,\"amount\":75},{\"min\":11,\"max\":2147483647,\"amount\":70}]},{\"warehouse\":\"RFD4\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":65},{\"min\":4,\"max\":6,\"amount\":65},{\"min\":7,\"max\":10,\"amount\":55},{\"min\":11,\"max\":2147483647,\"amount\":50}]},{\"warehouse\":\"JVL1\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":65},{\"min\":4,\"max\":6,\"amount\":65},{\"min\":7,\"max\":10,\"amount\":55},{\"min\":11,\"max\":2147483647,\"amount\":50}]},{\"warehouse\":\"MDW2\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":65},{\"min\":4,\"max\":6,\"amount\":55},{\"min\":7,\"max\":10,\"amount\":45},{\"min\":11,\"max\":2147483647,\"amount\":40}]},{\"warehouse\":\"MDW6\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":65},{\"min\":4,\"max\":6,\"amount\":55},{\"min\":7,\"max\":10,\"amount\":45},{\"min\":11,\"max\":2147483647,\"amount\":40}]},{\"warehouse\":\"MDW8\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":65},{\"min\":4,\"max\":6,\"amount\":55},{\"min\":7,\"max\":10,\"amount\":45},{\"min\":11,\"max\":2147483647,\"amount\":40}]},{\"warehouse\":\"MDW9\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":65},{\"min\":4,\"max\":6,\"amount\":55},{\"min\":7,\"max\":10,\"amount\":45},{\"min\":11,\"max\":2147483647,\"amount\":40}]},{\"warehouse\":\"ORD2\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":65},{\"min\":4,\"max\":6,\"amount\":55},{\"min\":7,\"max\":10,\"amount\":45},{\"min\":11,\"max\":2147483647,\"amount\":40}]},{\"warehouse\":\"STL4\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":85},{\"min\":4,\"max\":6,\"amount\":80},{\"min\":7,\"max\":10,\"amount\":75},{\"min\":11,\"max\":2147483647,\"amount\":70}]},{\"warehouse\":\"STL3\",\"areaPriceList\":[{\"min\":1,\"max\":3,\"amount\":85},{\"min\":4,\"max\":6,\"amount\":80},{\"min\":7,\"max\":10,\"amount\":75},{\"min\":11,\"max\":2147483647,\"amount\":70}]}]}");
        configMap.put("休斯顿", XSD_PRICE);
        configMap.put("纽约", DELIVERY_PRICE_CONFIG);
        nameMap.put("NYC", "纽约");
        nameMap.put("CHI", "芝加哥");
        nameMap.put("HOU", "休斯顿");


    }

    public static String getName(String zwArea) {
        for (Map.Entry<String, String> e : nameMap.entrySet()) {
            if (StringUtils.equals(e.getValue(), zwArea)) {
                return e.getKey();
            }
        }
        return null;
    }


    public static BigDecimal getFullCarPrice(String area, String warehouse) {
        return getAreaWarehouseSellMap().get(nameMap.get(area))
                .getWarehouseAreaList().stream().filter(s -> StringUtils.equals(warehouse, s.getWarehouse()))
                .filter(s -> s.getFullPrice() != null)
                .map(WarehouseArea::getFullPrice).findFirst().orElse(null);

    }

    public static String getConfig(String area) {
        return configMap.get(area);
    }

    public static Collection<String> getDeliveryPriceArea() {
        return configMap.keySet();
    }

    public static BigDecimal getTrailerFee(String area) {
        return trailerFeeMap.get(area);
    }


    public static Map<String, AreaWarehouseSell> getAreaWarehouseSellMap() {
        return configMap.values().stream()
                .map(s -> JsonUtil.deserialize(s, AreaWarehouseSell.class))
                .collect(Collectors.toMap(AreaWarehouseSell::getArea, Function.identity()));
    }

}
